using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_UserManagement : admin_BaseUserControl
{
    UserDAO daoUser = new UserDAO();
    int personId=0;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (GetCurrentUserRole()==1) cmbRole.Items.Add(new ListItem("Administrator", "1"));
        cmbRole.Items.Add(new ListItem("Manager", "2"));
        cmbRole.Items.Add(new ListItem("Worker", "3"));
        cmbRole.Items.Add(new ListItem("Customer", "10"));
        try
        {
            if (Request["per_id"] != null)
            {
                personId = int.Parse(Request["per_id"]);
                UserDAO daoUser = new UserDAO();
                if (daoUser.CheckOwner(personId).Id > 0)
                {
                    ShowMessage("These is at least one account of this person. Are you sure want to create another one?");
                }
            }
            else
            {
                Response.Redirect("Default.aspx?crtl=person");
            }
        }
        catch (Exception ex)
        {
     
            Response.Redirect("Default.aspx?ctrl=person");
        }
    }
    protected void lnkAdd_Click(object sender, EventArgs e)
    {
        this.Page.Validate();
        if (!this.Page.IsValid) return;
        User user = new User();
        user.UserName = txtUsername.Text;
        user.Password =CypherTextUtil.MD5Encrypt( txtPassword.Text);
        user.RegisteredDate = DateTime.Now;
        user.Role = int.Parse(cmbRole.SelectedValue);
        user.PersonInfor = new Person();
        user.PersonInfor.Id = personId;
        user.LastLogin = DateTime.Today;
        if (daoUser.Insert(user) > 0)
        {
            ShowMessage("New account have created successfully");
        }
        else ShowMessage("Error when trying to create new account. Action is aborted");
    }
    protected void lnkDelete_Click(object sender, EventArgs e)
    {

    }
}
